package com.LittleHan.crm.web.filter;

import com.LittleHan.crm.settings.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)res;
        HttpSession session=request.getSession();
        String path=request.getServletPath();
        User user=(User)session.getAttribute("user");
        if(user!=null){
           filterChain.doFilter(req,res);
        }
        else{
            if("/login.jsp".equals(path) || "/login.do".equals(path) || "/settings/user/toLogin.do".equals(path) || "/settings/user/cookie.do".equals(path)){
                /*if("/login.jsp".equals(path)){
                    filterChain.doFilter(req,res);
                    request.getRequestDispatcher("/settings/user/cookie.do").forward(req,res);
                    return;
                }*/
                filterChain.doFilter(req,res);
                return;
            }
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
